




//
//  HMTabBar.m
//  黑马微博-wzy
//
//  Created by Mac on 2016/12/8.
//  Copyright © 2016年 Mac. All rights reserved.
//

#import "HMTabBar.h"

@interface HMTabBar()

@property (nonatomic, strong) UIButton *plusButton;

@end

@implementation HMTabBar

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        //添加加号 按钮
        [self addPlusButton];
    }
    return self;
}

- (void)addPlusButton{
    
    UIButton *plusButton = [[UIButton alloc] init];
    //设置 按钮 背景
    [plusButton setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
    [plusButton setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
    //设置 按钮 前景
    [plusButton setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
    [plusButton setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
    //方法
    [plusButton addTarget:self action:@selector(plusButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:plusButton];
    self.plusButton = plusButton;

    
}




- (void)plusButtonClick:(UIButton *)button{
    NSLog(@"点击 加号 ===");
    if ([self.tabBarDelegate respondsToSelector:@selector(tabBarDelegateClickedPlusButton:)]) {
        [self.tabBarDelegate tabBarDelegateClickedPlusButton:self];
    }
    
    
}

- (void)layoutSubviews{
    [super layoutSubviews];
    
    self.tintColor = [UIColor orangeColor];
    
    //设置 tarButton的frame
    [self setupTabBarButtonFrame];
    
    //设置 加号按钮 frame
    [self setupPlusButtonFrame];

}


#pragma mark ======= 设置 tarButton的frame ======
- (void)setupTabBarButtonFrame{
    int index = 0;
    //计算 button 尺寸
    CGFloat buttonW = self.width / (self.items.count + 1);
    CGFloat buttonH = self.height;
    for (UIView *tabbarButton in self.subviews) {
        //如果不是UITabBarButton,直接跳过
        if (![tabbarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) continue;
        tabbarButton.width = buttonW;
        tabbarButton.height = buttonH;
        if (index >= 2) {
            tabbarButton.x = buttonW * (index + 1);
        }else{
            tabbarButton.x = buttonW * index;
        }
        
        tabbarButton.y = 0;
        index ++;
        

    }


}

#pragma mark --------- 设置 加号按钮 frame --------
- (void)setupPlusButtonFrame{
    self.plusButton.size = self.plusButton.currentBackgroundImage.size;
    self.plusButton.center = CGPointMake(self.width * 0.5, self.height * 0.5);

}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
